package com.alexwan.nightnote.ui.fragment;

import java.util.ArrayList;

import android.content.AsyncQueryHandler;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListView;
import cn.sharesdk.framework.ShareSDK;
import cn.sharesdk.onekeyshare.OnekeyShare;

import com.alexwan.nightnote.R;
import com.alexwan.nightnote.bean.NoteBean;
import com.alexwan.nightnote.db.ArticleProvider;
import com.alexwan.nightnote.db.ArticleProvider.ArticleConstants;
import com.alexwan.nightnote.ui.activity.AddArticleActivity;
import com.alexwan.nightnote.ui.adapter.NoteListAdapter;
import com.nhaarman.listviewanimations.appearance.simple.SwingBottomInAnimationAdapter;

/**
 * 主页
 * 
 * @author alexwan
 * 
 */
public class FragmentMain extends Fragment implements OnClickListener {
	private ListView mListView;
	private NoteListAdapter mListAdapter;
	private ArrayList<NoteBean> mNoteList = new ArrayList<NoteBean>();
	private NoteBean mNote;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_main, container, false);
	}

	private Button mShareBtn, mAddArticleBtn;
	private static final String[] PROJECTION_FROM = new String[] {
			ArticleConstants._ID, ArticleConstants.ARTICLE_TITLE,
			ArticleConstants.USER_NAME, ArticleConstants.ARTICLE_TIME,
			ArticleConstants.ARTICLE_CONTENT, ArticleConstants.ARTICLE_PIC };

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		initView();
		loadData();
	}

	private void initView() {
		// TODO Auto-generated method stub
		mShareBtn = (Button) getView().findViewById(R.id.share_content);
		mShareBtn.setOnClickListener(this);
		mAddArticleBtn = (Button) getView().findViewById(R.id.insert_article);
		mAddArticleBtn.setOnClickListener(this);
		mListView = (ListView) getView().findViewById(R.id.note_list);
		mListAdapter = new NoteListAdapter(getActivity());
		SwingBottomInAnimationAdapter mBottomInAnimAdapter = new SwingBottomInAnimationAdapter(
				mListAdapter);
		mBottomInAnimAdapter.setAbsListView(mListView);
		mListView.setAdapter(mBottomInAnimAdapter);
		mListAdapter.setList(mNoteList);
	}

	private void loadData() {
		new AsyncQueryHandler(getActivity().getContentResolver()) {

			@Override
			protected void onQueryComplete(int token, Object cookie,
					Cursor cursor) {

				getArticleInfo(cursor);
			}

		}.startQuery(0, null, ArticleProvider.CONETNT_URI, PROJECTION_FROM,
				null, null, null);
	}

	protected void getArticleInfo(Cursor cursor) {
		if (cursor != null && cursor.getCount() > 0) {
			Log.i("Night Note FragementMain", cursor.toString());

			for (int i = 0; i < cursor.getCount(); i++) {
				cursor.moveToPosition(i);
				mNote = new NoteBean();
				mNote.setmArticleDate(cursor.getString(cursor
						.getColumnIndex(ArticleConstants.ARTICLE_TIME)));
				mNote.setmArticleTitle(cursor.getString(cursor
						.getColumnIndex(ArticleConstants.ARTICLE_TITLE)));
				mNote.setmArticleContent(cursor.getString(cursor
						.getColumnIndex(ArticleConstants.ARTICLE_CONTENT)));
				mNoteList.add(mNote);
			}
			mNote = null;
			cursor.close();
		}
		mListAdapter.notifyDataSetChanged();
	}

	private void showShare() {
		ShareSDK.initSDK(getActivity());
		OnekeyShare oks = new OnekeyShare();
		// 关闭sso授权
		oks.disableSSOWhenAuthorize();

		// 分享时Notification的图标和文字
		oks.setNotification(R.drawable.ic_launcher,
				getString(R.string.app_name));
		// title标题，印象笔记、邮箱、信息、微信、人人网和QQ空间使用
		oks.setTitle(getString(R.string.share));
		// titleUrl是标题的网络链接，仅在人人网和QQ空间使用
		oks.setTitleUrl("http://sharesdk.cn");
		// text是分享文本，所有平台都需要这个字段
		oks.setText("我是分享文本");
		// imagePath是图片的本地路径，Linked-In以外的平台都支持此参数
		oks.setImagePath("/sdcard/test.jpg");// 确保SDcard下面存在此张图片
		// url仅在微信（包括好友和朋友圈）中使用
		oks.setUrl("http://sharesdk.cn");
		// comment是我对这条分享的评论，仅在人人网和QQ空间使用
		oks.setComment("我是测试评论文本");
		// site是分享此内容的网站名称，仅在QQ空间使用
		oks.setSite(getString(R.string.app_name));
		// siteUrl是分享此内容的网站地址，仅在QQ空间使用
		oks.setSiteUrl("http://sharesdk.cn");

		// 启动分享GUI
		oks.show(getActivity());
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.share_content:
			showShare();
			break;
		case R.id.insert_article:
			AddArticleActivity.launch(getActivity());
		default:
			break;
		}
	}
}
